package com.wyz.web.controller;


import com.wyz.common.dto.OrderDTO;
import com.wyz.web.mapper.UserRepository;
import com.wyz.web.service.OrderService;
import com.wyz.web.service.UserService;
import com.wyz.common.dto.UserDTO;
import com.wyz.common.util.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 手动通过session进行注册-登录-注销
 * 注意和session相关的代码都放在controller层
 */
@Slf4j
@RestController
@RequestMapping("/user")
//@CrossOrigin(origins = {"http://127.0.0.1","http://127.0.0.1/"}, allowCredentials = "true")
public class UserController {

    private static final String JSESSIONID = "JSESSIONID";

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpSession sess;

    // 测试页面
    @GetMapping("/hello")
    public String simpleString() {
        return "<h2>/hello</h2> <br>";
    }


    /**
     * 用户注册
     * <p>
     * 情况1: 用户名已经被注册了
     * 情况2: 正常注册
     */
    @PostMapping(value = "/register")
    public Message register(@RequestBody UserDTO u) {
        log.info("注册：{}", u);
        String msg = userService.addUser(u);

        if (!msg.equals("success")) {
            return Message.fail(msg);
        }

        // 创建一个session, 把Jsessionid发给前端。前端会跳回首页, 首页会负责查看有没有session
        Long userId = userRepository.getIdByUsername(u.getUsername());
        sess.setAttribute("userId", userId);
        Cookie cookie = new Cookie(JSESSIONID, sess.getId());
        response.addCookie(cookie);
        return Message.succeed("注册成功");
    }


    /**
     * 用户登录: 需要返回
     * <p>
     * 情况1: 用户名不存在
     * 情况2: 用户名存在, 但是密码错误
     * 情况3: 正确登录, 并注册一个session
     */
    @PostMapping(value = "/login")
    public Message login(@RequestBody UserDTO u) {
        log.info("登录：{}", u);
        String msg = userService.checkUserInfo(u);

        if (!msg.equals("success")) {
            return Message.fail(msg);
        }
        // 创建一个session, 把Jsessionid发给前端。前端会跳回首页, 首页会负责查看有没有session
        Long userId = userRepository.getIdByUsername(u.getUsername());
        sess.setAttribute("userId", userId);
        Cookie cookie = new Cookie(JSESSIONID, sess.getId());
        response.addCookie(cookie);
        return Message.succeed("登录成功");
    }

    /**
     * 注销
     */
    @RequestMapping(value = "/logout")
    public void logout() {
        log.info("注销：{}", sess.getAttribute("userId"));
        sess.invalidate();
    }

    /**
     * 检查用户: 前端发个请求看看用户是否登录
     * 情况1: 没有登录
     * 情况2: 已经登录
     */
    @RequestMapping(value = "/check")
    public Message check() {
        Object userId = sess.getAttribute("userId");
        if (userId == null) {
            return Message.fail("未登录");
        }
        return Message.succeed("已登录");
    }
}

